﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;
using MixScriptWeb.AppCode.gjips;
using MixScript.BLL.MM;
using MixScript.BLL.SD;
namespace MixScriptWeb.Jideco.MM
{
    public partial class fPO : cCommonWeb2, IMIxCommon
    {
        private static DataTable dtPO1;
        private static DataTable dtSupp;
        private static DateTime tglPO;
        protected void Page_Load(object sender, EventArgs e)
        {
            mn = (MainMenu2)this.Master;
            hideStatusBar();
            if (!Page.IsPostBack)
            {
                
                tglPO = DateTime.Now;
                addFindClickAttribute(this, new MixScriptTB[] { txtSupCode }, new string[] { "SRCSUPGJ" });
                //txtSupCode.Tbx.Attributes.Add("readonly", "readonly");
                addRead(new MixScriptTB[]{txtPONo});
                txtPODate.Text = tglPO.ToString("dd-MM-yyyy");
                MultiView1.SetActiveView(vwSelectPOtype);

            }            
        }

        #region IMIxCommon Members



        public bool isvalid()
        {
            try
            {
                if (txtPONo.Text.Trim() != string.Empty)
                {
                    return true;
                }
                else
                {
                    //ShowStatusBar("PO No must be Filled");
                    return true;
                }


                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion

        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {

                if (e.Row.DataItemIndex == -1)
                    return;

                Image img = (Image)e.Row.FindControl("imgSearch");
                TextBox tpono = (TextBox)e.Row.FindControl("txtPONO");
                TextBox tpartno = (TextBox)e.Row.FindControl("txtPartNo");
                TextBox tpartnm = (TextBox)e.Row.FindControl("txtPartName");
                TextBox tuom = (TextBox)e.Row.FindControl("txtUoM");
                TextBox tqty = (TextBox)e.Row.FindControl("txtQty");
                TextBox tamnt = (TextBox)e.Row.FindControl("txtAmount");


                //openWndChild(qrys, 480, 640);

                //img.Attributes.Add("onclick", "openWndChild('../popupPO/fm-"+this.Form.ClientID +",a-"+tpartno.ClientID+ ",b-"+tpartnm.ClientID+",c-"+tuom.ClientID+",d-"+tqty.ClientID+",e-"+tamnt.ClientID+",f-"+tpono.ClientID+",g-"+txtSupCode.Text+".html',480,640)");
                img.Attributes.Add("onclick", "openWndChild('../popupPO/fm-" + this.Form.ClientID + ",a-" + tpartno.ClientID + ",b-,c-,d-,e-,f-"+ddlPOType.SelectedValue+",g-" + txtSupCode.Text + ".html',480,640)");

                
                    if(ddlPOType.SelectedValue=="4")
                    {
                        TextBox tdt = (TextBox)e.Row.FindControl("txtUoM");
                        tdt.CssClass += " datepicker";
                        
                    }
                  
                //im.Attributes.Add("onclick", "browseitem('" + p.Form.ClientID + "', '" + m.ClientID + "_txt','" + m.ClientID + "_lbl','" + searchid[i] + "');");

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }
    
        protected void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSupCode.Text.Trim() != string.Empty)
                {
                    //if (ddlPOType.SelectedValue == "1")
                    //{
                    MultiView1.SetActiveView(View1);


                    dtPO1 = new DataTable();
                    dtPO1.Columns.Add("slno");
                    dtPO1.Columns.Add("nditno");
                    dtPO1.Columns.Add("itnm");
                    dtPO1.Columns.Add("orup");
                    dtPO1.Columns.Add("arndqt");
                    dtPO1.Columns.Add("tdam");
                    dtPO1.Columns.Add("fc1");
                    dtPO1.Columns.Add("fc2");
                    dtPO1.Columns.Add("fc3");
                    dtPO1.Columns.Add("fc4");
                    dtPO1.Columns.Add("frdvdt");
                    dtPO1.Columns.Add("dldate");


                    DataRow dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dtPO1.AcceptChanges();

                    gvPO1.DataSource = null;
                    gvPO2.DataSource = null;
                    gvPO3.DataSource = null;
                    gvPO4.DataSource = null;

                    if (ddlPOType.SelectedValue == "1")
                    {
                        gvPO1.DataSource = dtPO1;
                        gvPO1.DataBind();
                    }
                    else if(ddlPOType.SelectedValue=="2")
                    {
                        gvPO2.DataSource = dtPO1;
                        gvPO2.DataBind();
                    }
                    else if (ddlPOType.SelectedValue == "3")
                    {
                        gvPO3.DataSource = dtPO1;
                        gvPO3.DataBind();
                    }
                    else if (ddlPOType.SelectedValue == "4")
                    {
                        gvPO4.DataSource = dtPO1;
                        gvPO4.DataBind();
                    }

                    cSupplier oSup = new cSupplier();
                    dtSupp = new DataTable();
                    oSup.getSupp(txtSupCode.Text, ref dtSupp);
                    lblSupplier.Text = txtSupCode.Text + " - " + dtSupp.Rows[0]["bpidnm"];

                    // }
                }
                else
                {
                    ShowStatusBar("Please Select Supplier");
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (ddlPOType.SelectedValue == "1")
                {
                    settodt(gvPO1);
                }
                else if(ddlPOType.SelectedValue=="2")
                {
                    settodt(gvPO2);
                }
                else if (ddlPOType.SelectedValue == "3")
                {
                    settodt(gvPO3);
                }
                else if (ddlPOType.SelectedValue == "4")
                {
                    settodt(gvPO4);
                }
                ImageButton imb = (ImageButton)sender;
                string s = imb.ClientID;
                s = s.Replace("ContentPlaceHolder1_gvPO"+ddlPOType.SelectedValue+"_imgDelete_", "");
                int i = 0;
                int.TryParse(s, out i);
                dtPO1.Rows[i].Delete();
                dtPO1.AcceptChanges();
                if (ddlPOType.SelectedValue == "1")
                {
                    gvPO1.DataSource = dtPO1;
                    gvPO1.DataBind();
                }
                else if(ddlPOType.SelectedValue=="2")
                {
                    gvPO2.DataSource = dtPO1;
                    gvPO2.DataBind();
                }
                else if (ddlPOType.SelectedValue == "3")
                {
                    gvPO3.DataSource = dtPO1;
                    gvPO3.DataBind();
                }
                else if (ddlPOType.SelectedValue == "4")
                {
                    gvPO4.DataSource = dtPO1;
                    gvPO4.DataBind();
                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        private void settodt(GridView grv)
        {
            try
            {
                int c = 0;

                foreach (GridViewRow gv in grv.Rows)
                {
                    TextBox tb = (TextBox)gv.Cells[2].FindControl("txtPONO");
                    dtPO1.Rows[c]["slno"] = tb.Text;

                    tb = (TextBox)gv.Cells[2].FindControl("txtPartNo");
                    dtPO1.Rows[c]["nditno"] = tb.Text;
                    tb = (TextBox)gv.Cells[3].FindControl("txtPartName");
                    dtPO1.Rows[c]["itnm"] = tb.Text;
                    if (ddlPOType.SelectedValue != "4")
                    {
                        tb = (TextBox)gv.Cells[4].FindControl("txtUoM");
                        dtPO1.Rows[c]["orup"] = tb.Text;
                        tb = (TextBox)gv.Cells[6].FindControl("txtAmount");
                        dtPO1.Rows[c]["tdam"] = tb.Text;
                    }
                    tb = (TextBox)gv.Cells[5].FindControl("txtQty");
                    dtPO1.Rows[c]["arndqt"] = tb.Text;
         


                    tb = (TextBox)gv.Cells[7].FindControl("txtForecast1");
                    dtPO1.Rows[c]["fc1"] = tb.Text;
                    if (ddlPOType.SelectedValue == "1" || ddlPOType.SelectedValue=="3"||ddlPOType.SelectedValue=="4")
                    {
                        tb = (TextBox)gv.Cells[8].FindControl("txtForecast2");
                        dtPO1.Rows[c]["fc2"] = tb.Text;
                        tb = (TextBox)gv.Cells[9].FindControl("txtForecast3");
                        dtPO1.Rows[c]["fc3"] = tb.Text;
                        if (ddlPOType.SelectedValue != "4")
                        {
                            tb = (TextBox)gv.Cells[10].FindControl("txtForecast4");
                            dtPO1.Rows[c]["fc4"] = tb.Text;
                        }
                        else
                        {
                            tb = (TextBox)gv.Cells[9].FindControl("txtForecast4");
                            dtPO1.Rows[c]["frdvdt"] = tb.Text;
                            tb = (TextBox)gv.Cells[9].FindControl("txtUoM");
                            dtPO1.Rows[c]["dldate"] = tb.Text;
                        }
                    }
                    

                    c += 1;
                }

                dtPO1.AcceptChanges();

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }


        protected void btnAddNewRow_Click(object sender, EventArgs e)
        {
            try
            {

                if (ddlPOType.SelectedValue == "1")
                {
                    settodt(gvPO1);
                    DataRow dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dtPO1.AcceptChanges();
                    gvPO1.DataSource = dtPO1;
                    gvPO1.DataBind();
                }
                else if (ddlPOType.SelectedValue == "2")
                {
                    settodt(gvPO2);
                    DataRow dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dtPO1.AcceptChanges();
                    gvPO2.DataSource = dtPO1;
                    gvPO2.DataBind();
                }
                else if (ddlPOType.SelectedValue == "3")
                {
                    settodt(gvPO3);
                    DataRow dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dtPO1.AcceptChanges();
                    gvPO3.DataSource = dtPO1;
                    gvPO3.DataBind();
                }
                else if (ddlPOType.SelectedValue == "4")
                {
                    settodt(gvPO4);
                    DataRow dr = dtPO1.NewRow();
                    dtPO1.Rows.Add(dr);
                    dtPO1.AcceptChanges();
                    gvPO4.DataSource = dtPO1;
                    gvPO4.DataBind();
                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                if (isvalid())
                {
                    if (ddlPOType.SelectedValue == "1")
                    {
                        settodt(gvPO1);
                    }
                    else if(ddlPOType.SelectedValue=="2")
                    {
                        settodt(gvPO2);
                    }
                    else if (ddlPOType.SelectedValue == "3")
                    {
                        settodt(gvPO3);
                    }
                    else if (ddlPOType.SelectedValue == "4")
                    {
                        settodt(gvPO4);
                    }

                    
                    string sout = "";
                    cPO oPO = new cPO();
                    if (!oPO.isRecordExist("tb_pomst", new string[] { "pono" }, new string[] { txtPONo.Text }))
                    {
                        //oPO.Spono = txtPONo.Text;
                        oPO.Spayid = txtPayment.SelectedValue; //ddlPayment.SelectedValue;
                        oPO.Scodfbpid = txtSupCode.Text;
                        oPO.Spotyp = ddlPOType.SelectedValue;
                        oPO.Satt = txtAttn.Text;
                        oPO.TglPO = DateTime.Parse(toMDY(txtPODate.Text));
                        oPO.Scc = txtCC.Text;
                        decimal dsub = 0;
                        decimal dtemp = 0;
                        foreach (DataRow dr in dtPO1.Rows)
                        {
                            if (dr["slno"].ToString().Trim() != string.Empty)
                            {
                                decimal.TryParse(dr["tdam"].ToString(), out dtemp);
                                dsub += dtemp;
                            }
                        }
                        oPO.Dsubtot = dsub;
                        oPO.Dvat = dsub / 10;
                        oPO.Dtot = dsub + oPO.Dvat;
                        string sscript = "";
                        if (oPO.savePO(dtPO1, out sout, cBaseBLL.statustrx.create, getUserLogin()))
                        {
                            sscript += "<script>";
                            if (ddlPOType.SelectedValue == "1")
                            {
                                sscript += "window.open('../print/rptid-2,spono-" + oPO.Spono + ",suppid-" + txtSupCode.Text + ",dl.html', '_blank');";
                            }
                            else if (ddlPOType.SelectedValue == "2")
                            {
                               sscript += "window.open('../print/rptid-3,spono-" + oPO.Spono + ",suppid-" + txtSupCode.Text + ",dl.html', '_blank');";
                            }
                            else if (ddlPOType.SelectedValue == "3")
                            {
                                sscript += "window.open('../print/rptid-4,spono-" + oPO.Spono + ",suppid-" + txtSupCode.Text + ",dl.html', '_blank');";
                            }
                            else
                            {
                                sscript += "window.open('../print/rptid-,spono-" + oPO.Spono + ",suppid-" + txtSupCode.Text + ",dl.html', '_blank');";
                            }

                            //Response.Write("alert('sfdfs');");
                            sscript += "</script>";
                            MultiView1.SetActiveView(vwSelectPOtype);
                          //  txtPONo.Text = oPO.Spono;
                            ClientScript.RegisterStartupScript(GetType(), "report", sscript);
                            ShowStatusBar("PO Created Successfully", msgType.msgSuccess);
                        }
                        else
                        {
                            ShowStatusBar(sout);
                        }

                    }
                    else
                    {
                        ShowStatusBar("PO Number Already Exist");
                    }

                }
                else
                {

                }


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }

        protected void txtPODate_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ShowStatusBar(DateTime.Now.ToString());
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
    }
}